// AddressController.java
package com.xgq.drink.controller;

import com.xgq.drink.entity.Address;
import com.xgq.drink.entity.R;
import com.xgq.drink.utils.JwtTokenUtil;
import com.xgq.drink.service.IAddressService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/address")
@Tag(name = "地址模块", description = "地址管理接口")
public class AddressController {

    @Autowired
    private IAddressService addressService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    // 从Token中获取当前用户ID
    private String getCurrentUserId(HttpServletRequest request) {
        return jwtTokenUtil.getUserIdFromToken(request.getHeader("Authorization"));
    }

    @PostMapping("/add")
    @Operation(summary = "新增地址")
    public R addAddress(@RequestBody Address address, HttpServletRequest request) {
        System.out.println("进");
        String token = request.getHeader("Authorization");
        String uId = jwtTokenUtil.getUserIdFromToken(token);
        System.out.println(uId);
        return addressService.addAddress(address, uId);
    }

    @PutMapping("/update")
    @Operation(summary = "更新地址")
    public R updateAddress(@RequestBody Address address, HttpServletRequest request) {
        System.out.println("-----------");
        System.out.println(address);
        return addressService.updateAddress(address, Long.valueOf(getCurrentUserId(request)));
    }

    @GetMapping("/list")
    @Operation(summary = "获取地址列表")
    public R getAddressList(HttpServletRequest request) {
        System.out.println("获取地址列表");
        return addressService.getAddressList(Long.valueOf(getCurrentUserId(request)));
    }
}